{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   BB  AA      CC    EE    DD\n",
      "0  11   3  3230.0  45.6  20.0\n",
      "1   4   2  2124.0  67.0   NaN\n",
      "2   7  23   345.0  33.9  23.0\n",
      "3   5  11  2361.0  59.5   4.0\n",
      "4  10  45   326.0  69.9  55.0\n",
      "5  33  33     NaN  75.0  67.0\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "data_path=\"sort.txt\"\n",
    "# 解析数据\n",
    "data = pd.read_csv(data_path,sep=',',engine='python')\n",
    "print(data)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 对索引进行排序\n",
    "- sort_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   BB  AA      CC    EE    DD\n",
      "5  33  33     NaN  75.0  67.0\n",
      "4  10  45   326.0  69.9  55.0\n",
      "3   5  11  2361.0  59.5   4.0\n",
      "2   7  23   345.0  33.9  23.0\n",
      "1   4   2  2124.0  67.0   NaN\n",
      "0  11   3  3230.0  45.6  20.0\n"
     ]
    }
   ],
   "source": [
    "# 1.按行索引进行降序排列 ，纵向 索引 5，4，3，2，1，0\n",
    "data_res=data.sort_index(axis=0,ascending=False)\n",
    "print(data_res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     EE    DD      CC  BB  AA\n",
      "0  45.6  20.0  3230.0  11   3\n",
      "1  67.0   NaN  2124.0   4   2\n",
      "2  33.9  23.0   345.0   7  23\n",
      "3  59.5   4.0  2361.0   5  11\n",
      "4  69.9  55.0   326.0  10  45\n",
      "5  75.0  67.0     NaN  33  33\n"
     ]
    }
   ],
   "source": [
    "# 2.按列索引进行降序排列 ，横向 索引 EE DD CC BB AA\n",
    "data_res=data.sort_index(axis=1,ascending=False)\n",
    "print(data_res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   AA  BB      CC    DD    EE\n",
      "0   3  11  3230.0  20.0  45.6\n",
      "1   2   4  2124.0   NaN  67.0\n",
      "2  23   7   345.0  23.0  33.9\n",
      "3  11   5  2361.0   4.0  59.5\n",
      "4  45  10   326.0  55.0  69.9\n",
      "5  33  33     NaN  67.0  75.0\n"
     ]
    }
   ],
   "source": [
    "# 3.按列索引进行降序排列 ，横向 索引 A　B　C　D  E \n",
    "data_res=data.sort_index(axis=1,ascending=True)\n",
    "print(data_res)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 对数据进行排序\n",
    "- sort_values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   BB  AA      CC    EE    DD\n",
      "1   4   2  2124.0  67.0   NaN\n",
      "0  11   3  3230.0  45.6  20.0\n",
      "3   5  11  2361.0  59.5   4.0\n",
      "2   7  23   345.0  33.9  23.0\n",
      "5  33  33     NaN  75.0  67.0\n",
      "4  10  45   326.0  69.9  55.0\n"
     ]
    }
   ],
   "source": [
    "# 1.按 AA 列进行 行数据的 升序 排序\n",
    "data_res=data.sort_values(by=[\"AA\"],ascending=True,axis=0)\n",
    "print(data_res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   BB  AA      CC    EE    DD\n",
      "5  33  33     NaN  75.0  67.0\n",
      "4  10  45   326.0  69.9  55.0\n",
      "2   7  23   345.0  33.9  23.0\n",
      "0  11   3  3230.0  45.6  20.0\n",
      "3   5  11  2361.0  59.5   4.0\n",
      "1   4   2  2124.0  67.0   NaN\n"
     ]
    }
   ],
   "source": [
    "# 2.按 DD 列进行 行数据的 降序 排序,默认缺失值最后\n",
    "data_res=data.sort_values(by=[\"DD\"],ascending=False,axis=0)\n",
    "print(data_res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   BB  AA      CC    EE    DD\n",
      "1   4   2  2124.0  67.0   NaN\n",
      "5  33  33     NaN  75.0  67.0\n",
      "4  10  45   326.0  69.9  55.0\n",
      "2   7  23   345.0  33.9  23.0\n",
      "0  11   3  3230.0  45.6  20.0\n",
      "3   5  11  2361.0  59.5   4.0\n"
     ]
    }
   ],
   "source": [
    "# 3.na_position='first' 参数 ，按 DD 列进行 行数据的 降序 排序，缺失值 为首\n",
    "data_res=data.sort_values(by=[\"DD\"],ascending=False,axis=0,na_position='first')\n",
    "print(data_res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       CC    EE  AA  BB    DD\n",
      "0  3230.0  45.6   3  11  20.0\n",
      "1  2124.0  67.0   2   4   NaN\n",
      "2   345.0  33.9  23   7  23.0\n",
      "3  2361.0  59.5  11   5   4.0\n",
      "4   326.0  69.9  45  10  55.0\n",
      "5     NaN  75.0  33  33  67.0\n"
     ]
    }
   ],
   "source": [
    "# 4.横向，对索引3 排序\n",
    "data_res=data.sort_values(by=[3],ascending=False,axis=1)\n",
    "print(data_res)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据排名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    BB   AA   CC   EE   DD\n",
      "0  5.0  2.0  5.0  2.0  2.0\n",
      "1  1.0  1.0  3.0  4.0  NaN\n",
      "2  3.0  4.0  2.0  1.0  3.0\n",
      "3  2.0  3.0  4.0  3.0  1.0\n",
      "4  4.0  6.0  1.0  5.0  4.0\n",
      "5  6.0  5.0  NaN  6.0  5.0\n"
     ]
    }
   ],
   "source": [
    "# 1. 默认的 axis=0，在列上 体现数据排名\n",
    "data_res=data.rank()\n",
    "print(data_res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    BB   AA   CC   EE   DD\n",
      "0  2.0  1.0  5.0  4.0  3.0\n",
      "1  2.0  1.0  4.0  3.0  NaN\n",
      "2  1.0  2.5  5.0  4.0  2.5\n",
      "3  2.0  3.0  5.0  4.0  1.0\n",
      "4  1.0  2.0  5.0  4.0  3.0\n",
      "5  1.5  1.5  NaN  4.0  3.0\n"
     ]
    }
   ],
   "source": [
    "# 2. 设置的 axis=1，横向排名\n",
    "data_res=data.rank(axis=1)\n",
    "print(data_res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    BB   AA   CC   EE   DD\n",
      "0  2.0  1.0  5.0  4.0  3.0\n",
      "1  2.0  1.0  4.0  3.0  NaN\n",
      "2  1.0  2.0  5.0  4.0  3.0\n",
      "3  2.0  3.0  5.0  4.0  1.0\n",
      "4  1.0  2.0  5.0  4.0  3.0\n",
      "5  1.0  2.0  NaN  4.0  3.0\n"
     ]
    }
   ],
   "source": [
    "# 3. 设置 method=‘first’，相同数据，排名第一个为第一  ，如 第五行 第一第二个数据相同，第一个为1\n",
    "data_res=data.rank(axis=1,method='first')\n",
    "print(data_res)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.1"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "d6ee8951ea1f1e54d5c6db37d467cbab5e82ba9505f1ada5124c3f7e97e9c192"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
